#coding:utf-8
#6-1-11 作者：邓恩凡 ef-hz@qq.com
import pygame,time,random,sys
from pygame.locals import *
from sys import exit
background_image_filename = 'background.png'
paota = 'paota.png'
paodan='paodan.png'
paodan1='paodan.png'
paodan2='paodan.png'
paodan3='paodan.png'
paodan4='paodan.png'
biaoti='biaoti.png'
guangbiao='guangbiao.png'
hongzhaji1='hongzhaji.png'
hongzhaji2='hongzhaji.png'
hongzhaji3='hongzhaji.png'
hongzhaji4='hongzhaji.png'
hongzhaji5='hongzhaji.png'

print('#6-1-11 作者：邓恩凡 ef-hz@qq.com')
#初始化pygame，为使用硬件做准备
pygame.init()
# 创建一个窗口
screen = pygame.display.set_mode((750, 480), 0, 32)
#炮塔xy:
px=600
py=450
dx=0
dy=0
mz=1
start=0
#分数：
TF1_=0
#血量
TF2_=5
#子弹数量
TF3_=10+0
hjz_y=random.randint(0,330)
pd=0
tuichu=0
# 设置窗口标题
pygame.display.set_caption('炮塔模拟器')
class Scdj():
    def __init__(self,hjz_y):
        self.hjz_x=-50
        self.hjz_y=hjz_y
    def scdj(self,hjz_y):
        global TF1_, TF2_, TF3_
        self.hjz_y=hjz_y
        x_=self.hjz_x-30
        x__=self.hjz_x+30
        y_=self.hjz_y-30
        y__=self.hjz_y+30

        if  not dx>=x_ or not dx<=x__ or not dy>=y_ or not dy<=y__:
            #print('test')
            screen.blit(hongzhaji_list[random.randint(0, 4)], (self.hjz_x, self.hjz_y))
            self.hjz_x+=3
            if self.hjz_x >= 750:
                TF2_ -= 1
                self.hjz_x=-50
        else:
            self.hjz_x=-50
            self.hjz_y+=random.randint(-100,100)
            TF1_+=10
            if hjz_y<=0:
                hjz_y+=150
            if hjz_y>=330:
                hjz_y-=150
            sj=0
            sj=random.randint(0,1)
            if sj==0:
                TF3_+=random.randint(1,2)


Scdj=Scdj(hjz_y)

# 加载图片并转换
background = pygame.image.load(background_image_filename)
paota = pygame.image.load(paota)
paodan = pygame.image.load(paodan)
paodan1 = pygame.image.load(paodan1)
paodan2 = pygame.image.load(paodan2)
paodan3 = pygame.image.load(paodan3)
paodan4 = pygame.image.load(paodan4)
biaoti = pygame.image.load(biaoti)
guangbiao = pygame.image.load(guangbiao)
hongzhaji1 = pygame.image.load(hongzhaji1)
hongzhaji2 = pygame.image.load(hongzhaji2)
hongzhaji3 = pygame.image.load(hongzhaji3)
hongzhaji4 = pygame.image.load(hongzhaji4)
hongzhaji5 = pygame.image.load(hongzhaji5)
hongzhaji_list=[hongzhaji1,hongzhaji2,hongzhaji3,hongzhaji4,hongzhaji5]
paodan_list=[paodan,paodan1,paodan2,paodan3,paodan4]
paodan_n=-1







# 游戏主循环
while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            exit()

    # 字体
    TF = pygame.font.SysFont("SimHei", 30)
    h = TF.render('按下空格开始游戏  SPACE', True, (225, 0, 0))
    TS = pygame.font.SysFont("SimHei", 30)
    s = TS.render('子弹不足！', True, (225, 0, 0))
    TS1 = pygame.font.SysFont("SimHei", 30)
    s1 = TS1.render('游戏结束！您的得分是：' + str(TF1_), True, (225, 0, 0))


    screen.blit(background, (0, 0))
    screen.blit(paota, (px,py))
    if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
        if start==0:
            start=1
            time.sleep(0.2)
        elif TF3_>=1:
            dx=px
            dy=py
            TF3_-=1
            paodan_n+=1
            screen.blit(paodan_list[paodan_n], (dx, dy))
            if paodan_n>=4:
                paodan_n=-1
            time.sleep(0.2)
        else:
            screen.blit(s,(230,200))
    # 获得鼠标位置
    #x, y = pygame.mouse.get_pos()
    # 计算光标左上角位置
    #x -= paota.get_width() / 2
    #y -= paota.get_height() / 2

    #print(x,y)
    if start==1:
        if TF2_<=0:
            screen.blit(s1,(250,250))
            #游戏结束
            tuichu+=0.01
            if tuichu>=3:
                sys.exit()
        else:
            TF1_=round(0.01 + TF1_, 2)
        TF1 = pygame.font.SysFont("SimHei", 30)
        h1 = TF1.render('分数:' + str(TF1_), True, (225, 0, 0))
        TF2 = pygame.font.SysFont("SimHei", 30)
        h2 = TF2.render('血量:' + str(TF2_), True, (225, 0, 0))
        TF3 = pygame.font.SysFont("SimHei", 30)
        h3 = TF3.render('子弹数量:' + str(TF3_), True, (225, 0, 0))
        screen.blit(h1, (530, 0))
        screen.blit(h2, (530, 50))
        screen.blit(h3, (530, 100))
        if mz>0:
            dx -= 4
            dy -= 4
            screen.blit(guangbiao, (px-mz*80,py-mz*100))
        else:
            dx -= 4
            screen.blit(guangbiao, (px-100,py-20))
        if event.type == pygame.KEYDOWN and event.key == pygame.K_0:
            mz=0
        if event.type == pygame.KEYDOWN and event.key == pygame.K_1:
            mz=1
        if event.type == pygame.KEYDOWN and event.key == pygame.K_2:
            mz=2
        if event.type == pygame.KEYDOWN and event.key == pygame.K_3:
            mz=3
        if event.type == pygame.KEYDOWN and event.key == pygame.K_d:
            if px<650:
                px+=1
                #time.sleep(0.001)
        if event.type == pygame.KEYDOWN and event.key == pygame.K_a:
            if px>=0:
                px-=1
                #time.sleep(0.001)
        screen.blit(paodan, (dx, dy))

        #战机生成：
        if pd>=2.58:
            pd=0
            hjz_y+=random.randint(-100,100)
            if hjz_y<=0:
                hjz_y+=150
            if hjz_y>=330:
                hjz_y-=150
        pd+=0.01
        Scdj.scdj(hjz_y)
    if start==0:
        screen.blit(biaoti,(75,0))
        screen.blit(h, (220, 250))

    # 刷新画面
    pygame.display.update()
